<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Chapter 2: Variables And Basic Types | A君的秘密基地</title><meta name="keywords" content="C++ Primer"><meta name="author" content="astralcon"><meta name="copyright" content="astralcon"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="# Exercise Section 2.1.1 Ex2.112345一个 long long 类型至少和 一个 long 类型一样大；一个 long 类型至少和一个 int 类型一样大；一个 int 类型至少和一个 short 类型一样大。unsigned 类型不存在负数；signed 类型存在负数。一个 double 类型至少和一个 float 类型一样大。  Exercises Secti">
<meta property="og:type" content="article">
<meta property="og:title" content="Chapter 2: Variables And Basic Types">
<meta property="og:url" content="https://astralcon.gitee.io/blog/2022/05/14/Chapter-2-Variables-And-Basic-Types/index.html">
<meta property="og:site_name" content="A君的秘密基地">
<meta property="og:description" content="# Exercise Section 2.1.1 Ex2.112345一个 long long 类型至少和 一个 long 类型一样大；一个 long 类型至少和一个 int 类型一样大；一个 int 类型至少和一个 short 类型一样大。unsigned 类型不存在负数；signed 类型存在负数。一个 double 类型至少和一个 float 类型一样大。  Exercises Secti">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-05-14T06:24:04.000Z">
<meta property="article:modified_time" content="2022-05-14T06:52:20.226Z">
<meta property="article:author" content="astralcon">
<meta property="article:tag" content="C++ Primer">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/blog/img/avatar.jpg"><link rel="canonical" href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-2-Variables-And-Basic-Types/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: {"appId":"YUB0ZFWBBL","apiKey":"78d0f1fae31a1df3e0cb573725c55558","indexName":"blog","hits":{"per_page":6},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Chapter 2: Variables And Basic Types',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-14 14:52:20'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">A君的秘密基地</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Chapter 2: Variables And Basic Types</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-14T06:24:04.000Z" title="发表于 2022-05-14 14:24:04">2022-05-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-14T06:52:20.226Z" title="更新于 2022-05-14 14:52:20">2022-05-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/C/">C++</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/blog/2022/05/14/Chapter-2-Variables-And-Basic-Types/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count" data-xid="/blog/2022/05/14/Chapter-2-Variables-And-Basic-Types/" itemprop="commentCount"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>﻿# Exercise Section 2.1.1</p>
<h2 id="Ex2-1"><a href="#Ex2-1" class="headerlink" title="Ex2.1"></a>Ex2.1</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">一个 long long 类型至少和 一个 long 类型一样大；一个 long 类型至少和一个 int 类型一样大；一个 int 类型至少和一个 short 类型一样大。</span><br><span class="line"></span><br><span class="line">unsigned 类型不存在负数；signed 类型存在负数。</span><br><span class="line"></span><br><span class="line">一个 double 类型至少和一个 float 类型一样大。</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-1-2"><a href="#Exercises-Section-2-1-2" class="headerlink" title="Exercises Section 2.1.2"></a>Exercises Section 2.1.2</h1><h2 id="Ex2-3-2-4"><a href="#Ex2-3-2-4" class="headerlink" title="Ex2.3 2.4"></a>Ex2.3 2.4</h2><p><img src="https://img-blog.csdnimg.cn/20210527164440167.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81MTcwMzE3NA==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<h1 id="Exercises-Section-2-1-3"><a href="#Exercises-Section-2-1-3" class="headerlink" title="Exercises Section 2.1.3"></a>Exercises Section 2.1.3</h1><h2 id="Ex2-5"><a href="#Ex2-5" class="headerlink" title="Ex2.5"></a>Ex2.5</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">a) <span class="string">&#x27;a&#x27;</span> 		<span class="comment">// 一个字符 a</span></span><br><span class="line">   <span class="string">L&#x27;a&#x27;</span> 	<span class="comment">// wchar_t 类型字符 a</span></span><br><span class="line">   <span class="string">&quot;a&quot;</span>		<span class="comment">// 字符串 a</span></span><br><span class="line">   <span class="string">L&quot;a&quot;</span>		<span class="comment">// wchar_t 类型字符串 a</span></span><br><span class="line">   </span><br><span class="line">b) <span class="number">10</span>  		<span class="comment">// 十进制 int 整数值 10</span></span><br><span class="line">   <span class="number">10u</span> 		<span class="comment">// 十进制 unsigned 类型整数值 10</span></span><br><span class="line">   <span class="number">10L</span>		<span class="comment">// 十进制 long 类型整数值 10</span></span><br><span class="line">   <span class="number">10uL</span>		<span class="comment">// 十进制 unsigend long 类型整数值 10</span></span><br><span class="line">   <span class="number">012</span>		<span class="comment">// 八进制整数值 10</span></span><br><span class="line">   <span class="number">0xC</span> 		<span class="comment">// 十六进制整数值 12</span></span><br><span class="line"> </span><br><span class="line">c) <span class="number">3.14</span>		<span class="comment">// 浮点数3.14</span></span><br><span class="line">   <span class="number">3.14f</span>	<span class="comment">// float 类型浮点数 3.14</span></span><br><span class="line">   <span class="number">3.14L</span>	<span class="comment">// long double 类型浮点数 3.14</span></span><br><span class="line">   </span><br><span class="line">d) <span class="number">10</span>		<span class="comment">// 十进制整数值 10</span></span><br><span class="line">   <span class="number">10u</span>		<span class="comment">// 十进制 unsigned 类型整数值 10</span></span><br><span class="line">   <span class="number">10.</span>		<span class="comment">// 浮点数 10.0</span></span><br><span class="line">   <span class="number">10e-2</span>	<span class="comment">// 科学计数法浮点数 0.01</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-6"><a href="#Ex2-6" class="headerlink" title="Ex2.6"></a>Ex2.6</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// month 和 day 都是十进制</span></span><br><span class="line"><span class="type">int</span> month = <span class="number">9</span>, day = <span class="number">7</span>;</span><br><span class="line"><span class="comment">// month 为无效的八进制，day 为八进制</span></span><br><span class="line"><span class="type">int</span> month = <span class="number">09</span>, day = <span class="number">07</span>;</span><br></pre></td></tr></table></figure>

<h2 id="Ex2-7"><a href="#Ex2-7" class="headerlink" title="Ex2.7"></a>Ex2.7</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">b) <span class="number">3.14e1</span>L		<span class="comment">// 科学计数法表示的 long double 类型浮点数 31.4</span></span><br><span class="line">c) <span class="number">1024f</span>		<span class="comment">// float 类型浮点数 1024.0</span></span><br><span class="line">d) <span class="number">3.14L</span>		<span class="comment">// long double 类型浮点数 3.14</span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-2-1"><a href="#Exercises-Section-2-2-1" class="headerlink" title="Exercises Section 2.2.1"></a>Exercises Section 2.2.1</h1><h2 id="Ex2-9"><a href="#Ex2-9" class="headerlink" title="Ex2.9"></a>Ex2.9</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">a) std::cin &gt;&gt; <span class="type">int</span> inut_value;	<span class="comment">// 非法，需要输入变量前定义变量</span></span><br><span class="line"><span class="comment">// 修正如下</span></span><br><span class="line"><span class="type">int</span> value;</span><br><span class="line">std::cin &gt;&gt; value;</span><br><span class="line"></span><br><span class="line">b) <span class="type">int</span> i = &#123; <span class="number">3.14</span> &#125;;	<span class="comment">// 非法，花括号初始化不能转换类型</span></span><br><span class="line"><span class="comment">// 修正如下</span></span><br><span class="line"><span class="type">int</span> i = <span class="number">3</span>;</span><br><span class="line"></span><br><span class="line">c) <span class="type">double</span> salary = wage = <span class="number">9999.99</span>;	<span class="comment">// 非法，wage 未定义</span></span><br><span class="line"><span class="comment">// 修正如下</span></span><br><span class="line"><span class="type">double</span> salaty = <span class="number">9999.99</span>, wage = <span class="number">9999.99</span>;</span><br><span class="line"></span><br><span class="line">d) <span class="type">int</span> i = <span class="number">3.14</span>	<span class="comment">// 合法，只是会将浮点数转换为 int 类型，i 的值为 3</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-10"><a href="#Ex2-10" class="headerlink" title="Ex2.10"></a>Ex2.10</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">std::string global_str;		<span class="comment">// 空字符串</span></span><br><span class="line"><span class="type">int</span> global_int;				<span class="comment">// 值为 0</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> local_int;			<span class="comment">// 取决于编译器</span></span><br><span class="line">    std::string local_str;	<span class="comment">// 取决于编译器</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-2-2"><a href="#Exercises-Section-2-2-2" class="headerlink" title="Exercises Section 2.2.2"></a>Exercises Section 2.2.2</h1><h2 id="Ex2-11"><a href="#Ex2-11" class="headerlink" title="Ex2.11"></a>Ex2.11</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a) <span class="keyword">extern</span> <span class="type">int</span> ix = <span class="number">1024</span>;		<span class="comment">// 声明并定义</span></span><br><span class="line">b) <span class="type">int</span> iy;					<span class="comment">// 声明</span></span><br><span class="line">c) <span class="keyword">extern</span> <span class="type">int</span> iz;			<span class="comment">// 声明</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-2-3"><a href="#Exercises-Section-2-2-3" class="headerlink" title="Exercises Section 2.2.3"></a>Exercises Section 2.2.3</h1><h2 id="Ex2-12"><a href="#Ex2-12" class="headerlink" title="Ex2.12"></a>Ex2.12</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a) <span class="type">int</span> <span class="type">double</span> = <span class="number">3.14</span>;		<span class="comment">// 非法，变量名不能和关键字名一样</span></span><br><span class="line">b) <span class="type">int</span> _;					<span class="comment">// 合法</span></span><br><span class="line">c) <span class="type">int</span> <span class="keyword">catch</span><span class="number">-22</span>;			<span class="comment">// 非法，变量中不能有 - 符号</span></span><br><span class="line">d) <span class="type">int</span> <span class="number">1</span>_or_2 = <span class="number">1</span>;			<span class="comment">// 非法，变量不能以数字开头</span></span><br><span class="line">e) <span class="type">double</span> Double = <span class="number">3.14</span>;	<span class="comment">// 合法</span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-2-4"><a href="#Exercises-Section-2-2-4" class="headerlink" title="Exercises Section 2.2.4"></a>Exercises Section 2.2.4</h1><h2 id="Ex2-13"><a href="#Ex2-13" class="headerlink" title="Ex2.13"></a>Ex2.13</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">j = 100</span><br></pre></td></tr></table></figure>

<h2 id="Ex2-14"><a href="#Ex2-14" class="headerlink" title="Ex2.14"></a>Ex2.14</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">合法，输出结果：100 45</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-3-1"><a href="#Exercises-Section-2-3-1" class="headerlink" title="Exercises Section 2.3.1"></a>Exercises Section 2.3.1</h1><h2 id="Ex2-15"><a href="#Ex2-15" class="headerlink" title="Ex2.15"></a>Ex2.15</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a) <span class="type">int</span> ival = <span class="number">1.01</span>;	<span class="comment">// 合法</span></span><br><span class="line">b) <span class="type">int</span> &amp;rvall = <span class="number">1.01</span>;	<span class="comment">// 非法，引用绑定的对象不能是字面值</span></span><br><span class="line">c) <span class="type">int</span> &amp;rval2 = ival;	<span class="comment">// 合法</span></span><br><span class="line">d) <span class="type">int</span> &amp;rval3;		<span class="comment">// 非法，引用必须绑定在一个对象上</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-16"><a href="#Ex2-16" class="headerlink" title="Ex2.16"></a>Ex2.16</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> i = <span class="number">0</span>, &amp;r1 = i; <span class="type">double</span> d = <span class="number">0</span>, &amp;r2 = d;</span><br><span class="line">a) r2 = <span class="number">3.14159</span>;	<span class="comment">// 合法</span></span><br><span class="line">b) r2 = r1;			<span class="comment">// 合法</span></span><br><span class="line">c) i = r2;			<span class="comment">// 合法</span></span><br><span class="line">d) r1 = d;			<span class="comment">// 合法</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-17"><a href="#Ex2-17" class="headerlink" title="Ex2.17"></a>Ex2.17</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">输出结果：10 10</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-3-2"><a href="#Exercises-Section-2-3-2" class="headerlink" title="Exercises Section 2.3.2"></a>Exercises Section 2.3.2</h1><h2 id="Ex2-18"><a href="#Ex2-18" class="headerlink" title="Ex2.18"></a>Ex2.18</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>, j = <span class="number">1</span>;</span><br><span class="line">	<span class="type">int</span> *p = &amp;i;</span><br><span class="line">	std::cout &lt;&lt; p &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; *p &lt;&lt; std::endl;</span><br><span class="line">	p = &amp;j;</span><br><span class="line">	*p = j;</span><br><span class="line">	std::cout &lt;&lt; p &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; *p &lt;&lt; std::endl;</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex2-19"><a href="#Ex2-19" class="headerlink" title="Ex2.19"></a>Ex2.19</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1. 指针是一个对象，而引用不是。</span><br><span class="line">2. 指针可以为空，而引用不能。</span><br></pre></td></tr></table></figure>

<h2 id="Ex2-20"><a href="#Ex2-20" class="headerlink" title="Ex2.20"></a>Ex2.20</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> i = <span class="number">42</span>;</span><br><span class="line"><span class="type">int</span> *p1 = &amp;i;</span><br><span class="line">*p1 = *p1 * *p1;	<span class="comment">// 将初始变量 i 的值平方然后赋给变量 i</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-21"><a href="#Ex2-21" class="headerlink" title="Ex2.21"></a>Ex2.21</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">a) <span class="type">double</span> *dp = &amp;i;		<span class="comment">// 非法，指针与变量类型不匹配</span></span><br><span class="line">b) <span class="type">int</span> *ip = i;			<span class="comment">// 非法，指针绑定的对象前应该加 &amp; 符号</span></span><br><span class="line">c) <span class="type">int</span> *p = &amp;i;			<span class="comment">// 合法</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-22"><a href="#Ex2-22" class="headerlink" title="Ex2.22"></a>Ex2.22</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(p)	<span class="comment">// 指针指向的变量的地址不为0，条件成立</span></span><br><span class="line"><span class="keyword">if</span> (*p)	<span class="comment">// 指针指向的变量的值不为0，条件成立</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-24"><a href="#Ex2-24" class="headerlink" title="Ex2.24"></a>Ex2.24</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void 类型指针可以指向绑定任何类型对象，但是其他类型指针只能绑定同类对象。</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-3-3"><a href="#Exercises-Section-2-3-3" class="headerlink" title="Exercises Section 2.3.3"></a>Exercises Section 2.3.3</h1><h2 id="Ex2-25"><a href="#Ex2-25" class="headerlink" title="Ex2.25"></a>Ex2.25</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a) ip 为 <span class="type">int</span> 类型指针，值不确定(编译器决定)；i 为 <span class="type">int</span> 类型变量，值不确定(同前)；r 为 <span class="type">int</span> 类型引用绑定到变量 i 上，值和 i 的值一样。</span><br><span class="line">b) 变量 i 为 <span class="type">int</span> 类型，值不确定； ip 为 <span class="type">int</span> 类型指针，值为 <span class="number">0</span>。</span><br><span class="line">c) ip 为 <span class="type">int</span> 类型指针，值不确定；ip2 为 <span class="type">int</span> 类型变量，值不确定。</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-4"><a href="#Exercises-Section-2-4" class="headerlink" title="Exercises Section 2.4"></a>Exercises Section 2.4</h1><h2 id="Ex2-26"><a href="#Ex2-26" class="headerlink" title="Ex2.26"></a>Ex2.26</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a) <span class="type">const</span> <span class="type">int</span> buf;	<span class="comment">// 非法，const 修饰的变量必须初始化</span></span><br><span class="line">b) <span class="type">int</span> cnt = <span class="number">0</span>;		<span class="comment">// 合法</span></span><br><span class="line">c) <span class="type">const</span> in sz = cnt;	<span class="comment">// 合法</span></span><br><span class="line">d) ++cnt; 	<span class="comment">// 合法   </span></span><br><span class="line">   ++sz;	<span class="comment">// 非法，被 const 修饰的对象的值不能更改</span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-4-2"><a href="#Exercises-Section-2-4-2" class="headerlink" title="Exercises Section 2.4.2"></a>Exercises Section 2.4.2</h1><h2 id="EX2-27"><a href="#EX2-27" class="headerlink" title="EX2.27"></a>EX2.27</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a) <span class="type">int</span> i = <span class="number">-1</span>, &amp;r = <span class="number">0</span>;	<span class="comment">// 变量 i 合法，引用 r 非法，引用不能绑定到字面值上</span></span><br><span class="line">b) <span class="type">int</span> *<span class="type">const</span> p2 = &amp;i2;	<span class="comment">// 合法</span></span><br><span class="line">c) <span class="type">const</span> <span class="type">int</span> i = <span class="number">-1</span>, &amp;r = <span class="number">0</span>;	<span class="comment">// 变量 i 合法，引用 r 非法，引用不能绑定到字面值上</span></span><br><span class="line">d) cnost <span class="type">int</span> *<span class="type">const</span> p3 = &amp;i2;	<span class="comment">// 合法</span></span><br><span class="line">e) <span class="type">const</span> <span class="type">int</span> *p1 = &amp;i2;		<span class="comment">// 合法</span></span><br><span class="line">f) <span class="type">const</span> <span class="type">int</span> &amp;<span class="type">const</span> r2;		<span class="comment">// 非法，引用 r2 必须绑定一个对象</span></span><br><span class="line">g) <span class="type">const</span> <span class="type">int</span> i2 = i, &amp;r = i;	<span class="comment">//	合法</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-28"><a href="#Ex2-28" class="headerlink" title="Ex2.28"></a>Ex2.28</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a) <span class="type">int</span> i, *<span class="type">const</span> cp;	<span class="comment">// 指针 cp 非法，必须初始化</span></span><br><span class="line">b) <span class="type">int</span> *p1, *<span class="type">const</span> p2;	<span class="comment">// 指针 p2 非法，必须初始化</span></span><br><span class="line">c) <span class="type">const</span> <span class="type">int</span> ic, &amp;r = ic;	<span class="comment">// 变量 ic 非法，必须初始化</span></span><br><span class="line">d) <span class="type">const</span> <span class="type">int</span> *<span class="type">const</span> p3;	<span class="comment">// 指针 p3 非法，必须初始化</span></span><br><span class="line">e) <span class="type">const</span> <span class="type">int</span> *p;	<span class="comment">// 合法</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-29"><a href="#Ex2-29" class="headerlink" title="Ex2.29"></a>Ex2.29</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">a) i = ic;		<span class="comment">// 合法</span></span><br><span class="line">b) p1 = p3;		<span class="comment">// 非法，普通指针不能绑定 const 修饰的对象</span></span><br><span class="line">c) p1 = &amp;ic;	<span class="comment">// 非法，普通指针不能绑定 const 修饰的对象</span></span><br><span class="line">d) p3 = &amp;ic;	<span class="comment">// 合法</span></span><br><span class="line">e) p2 = p1;		<span class="comment">// 合法</span></span><br><span class="line">f) ic = *p3;	<span class="comment">// 非法，被 const 修饰的对象的值不能更改</span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-4-3"><a href="#Exercises-Section-2-4-3" class="headerlink" title="Exercises Section 2.4.3"></a>Exercises Section 2.4.3</h1><h2 id="Ex2-30"><a href="#Ex2-30" class="headerlink" title="Ex2.30"></a>Ex2.30</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">int</span> v2 = <span class="number">0</span>;	<span class="comment">// top-level</span></span><br><span class="line"><span class="type">int</span> v1 = v2;</span><br><span class="line"><span class="type">int</span> *p1 = &amp;v1, &amp;r1 = v1;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> *p2 = &amp;v2, *<span class="type">const</span> p3 = &amp;i, &amp;r2 = v2;	<span class="comment">// p2 -&gt; low-level  p3 -&gt; top-level and low-level     r2 -&gt; low-level</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-31"><a href="#Ex2-31" class="headerlink" title="Ex2.31"></a>Ex2.31</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">r1 = v2;	<span class="comment">// 非法，普通引用不能绑定被 const 修饰的对象</span></span><br><span class="line">p1 = p2;	<span class="comment">// 非法，p2 有 low-level const 而 p1 没有</span></span><br><span class="line">p2 = p1;	<span class="comment">// 合法</span></span><br><span class="line">p1 = p3;	<span class="comment">// 非法，p3 有 low-level const 而 p1 没有</span></span><br><span class="line">p2 = p3;	<span class="comment">// 合法</span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-4-4"><a href="#Exercises-Section-2-4-4" class="headerlink" title="Exercises Section 2.4.4"></a>Exercises Section 2.4.4</h1><h2 id="Ex2-32"><a href="#Ex2-32" class="headerlink" title="Ex2.32"></a>Ex2.32</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> null = <span class="number">0</span>, *p = null;	<span class="comment">// 非法</span></span><br><span class="line"><span class="comment">// 修改如下</span></span><br><span class="line"><span class="type">int</span> null = <span class="number">0</span>, *p = &amp;null;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-5-2"><a href="#Exercises-Section-2-5-2" class="headerlink" title="Exercises Section 2.5.2"></a>Exercises Section 2.5.2</h1><h2 id="Ex2-35"><a href="#Ex2-35" class="headerlink" title="Ex2.35"></a>Ex2.35</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">int</span> i = <span class="number">42</span>;</span><br><span class="line"><span class="keyword">auto</span> j = i; <span class="comment">// j 为 int 类型</span></span><br><span class="line"><span class="type">const</span> <span class="keyword">auto</span> &amp;k = i; <span class="comment">// k 为 const int&amp; 类型</span></span><br><span class="line"><span class="keyword">auto</span> *p = &amp;i;	<span class="comment">// p 为 const int* 类型</span></span><br><span class="line"><span class="type">const</span> <span class="keyword">auto</span> j2 = i, &amp;k2 = i;	<span class="comment">// j2 为 const int 类型， k2 为 const int&amp; 类型</span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-2-5-3"><a href="#Exercises-Section-2-5-3" class="headerlink" title="Exercises Section 2.5.3"></a>Exercises Section 2.5.3</h1><h2 id="Ex2-36"><a href="#Ex2-36" class="headerlink" title="Ex2.36"></a>Ex2.36</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">3</span>, b = <span class="number">4</span>;</span><br><span class="line"><span class="keyword">decltype</span> (a) c = a;	<span class="comment">// c 为 int 类型</span></span><br><span class="line"><span class="keyword">decltype</span> ((b)) d = a;	<span class="comment">// d 为 int&amp; 类型</span></span><br><span class="line">++c; <span class="comment">// c = 4	</span></span><br><span class="line">++d;	<span class="comment">// d = 4</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex2-37"><a href="#Ex2-37" class="headerlink" title="Ex2.37"></a>Ex2.37</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">3</span>, b = <span class="number">4</span>;</span><br><span class="line"><span class="keyword">decltype</span> (a) c = a;		<span class="comment">// c 为 int 类型，值为 3</span></span><br><span class="line"><span class="keyword">decltype</span> （a = b) d = a;	<span class="comment">// d 为 int&amp; 类型，值为 3</span></span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog">astralcon</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-2-Variables-And-Basic-Types/">https://astralcon.gitee.io/blog/2022/05/14/Chapter-2-Variables-And-Basic-Types/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://astralcon.gitee.io/blog" target="_blank">A君的秘密基地</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/C-Primer/">C++ Primer</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/gh/overtrue/share.js@master/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/blog/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/blog/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2022/05/14/Chapter-3-Strings%E3%80%81Vectors-And-Arrays/"><img class="prev-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Chapter 3: Strings、Vectors And Arrays</div></div></a></div><div class="next-post pull-right"><a href="/blog/2022/05/14/Chapter-1-Getting-Started/"><img class="next-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Chapter 1: Getting Started</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2022/05/14/Chapter-1-Getting-Started/" title="Chapter 1: Getting Started"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 1: Getting Started</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-10-Generic-Algorithms/" title="Chapter 10: Generic Algorithms"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 10: Generic Algorithms</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-11-Associative-Containers/" title="Chapter 11: Associative Containers"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 11: Associative Containers</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-12-Dynamic-Memory/" title="Chapter 12: Dynamic Memory"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 12: Dynamic Memory</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-13-Copy-Control/" title="Chapter 13: Copy Control"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 13: Copy Control</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/" title="Chapter 15: Object-Oriented Programming"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 15: Object-Oriented Programming</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/avatar.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">astralcon</div><div class="author-info__description">slow is fast.</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/astralcon" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:a842181471@qq.com.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">朝九晚五，健康生活！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-1"><span class="toc-number">1.</span> <span class="toc-text">Ex2.1</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-1-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.1.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-3-2-4"><span class="toc-number">1.</span> <span class="toc-text">Ex2.3 2.4</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-1-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.1.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-5"><span class="toc-number">1.</span> <span class="toc-text">Ex2.5</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-6"><span class="toc-number">2.</span> <span class="toc-text">Ex2.6</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-7"><span class="toc-number">3.</span> <span class="toc-text">Ex2.7</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-2-1"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.2.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-9"><span class="toc-number">1.</span> <span class="toc-text">Ex2.9</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-10"><span class="toc-number">2.</span> <span class="toc-text">Ex2.10</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-2-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.2.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-11"><span class="toc-number">1.</span> <span class="toc-text">Ex2.11</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-2-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.2.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-12"><span class="toc-number">1.</span> <span class="toc-text">Ex2.12</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-2-4"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.2.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-13"><span class="toc-number">1.</span> <span class="toc-text">Ex2.13</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-14"><span class="toc-number">2.</span> <span class="toc-text">Ex2.14</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-3-1"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.3.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-15"><span class="toc-number">1.</span> <span class="toc-text">Ex2.15</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-16"><span class="toc-number">2.</span> <span class="toc-text">Ex2.16</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-17"><span class="toc-number">3.</span> <span class="toc-text">Ex2.17</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-3-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.3.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-18"><span class="toc-number">1.</span> <span class="toc-text">Ex2.18</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-19"><span class="toc-number">2.</span> <span class="toc-text">Ex2.19</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-20"><span class="toc-number">3.</span> <span class="toc-text">Ex2.20</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-21"><span class="toc-number">4.</span> <span class="toc-text">Ex2.21</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-22"><span class="toc-number">5.</span> <span class="toc-text">Ex2.22</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-24"><span class="toc-number">6.</span> <span class="toc-text">Ex2.24</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-3-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.3.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-25"><span class="toc-number">1.</span> <span class="toc-text">Ex2.25</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-4"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-26"><span class="toc-number">1.</span> <span class="toc-text">Ex2.26</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-4-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.4.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#EX2-27"><span class="toc-number">1.</span> <span class="toc-text">EX2.27</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-28"><span class="toc-number">2.</span> <span class="toc-text">Ex2.28</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-29"><span class="toc-number">3.</span> <span class="toc-text">Ex2.29</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-4-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.4.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-30"><span class="toc-number">1.</span> <span class="toc-text">Ex2.30</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-31"><span class="toc-number">2.</span> <span class="toc-text">Ex2.31</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-4-4"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.4.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-32"><span class="toc-number">1.</span> <span class="toc-text">Ex2.32</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-5-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.5.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-35"><span class="toc-number">1.</span> <span class="toc-text">Ex2.35</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-2-5-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 2.5.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-36"><span class="toc-number">1.</span> <span class="toc-text">Ex2.36</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex2-37"><span class="toc-number">2.</span> <span class="toc-text">Ex2.37</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/17/%E3%80%8A%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E6%89%8B%E5%86%8C%E3%80%8B%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%9AChapter-1/" title="《算法设计手册》面试题：Chapter 1">《算法设计手册》面试题：Chapter 1</a><time datetime="2022-05-17T07:35:05.000Z" title="发表于 2022-05-17 15:35:05">2022-05-17</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-DHCP-v7-0/" title="Wireshark Lab: DHCP v7.0">Wireshark Lab: DHCP v7.0</a><time datetime="2022-05-14T07:19:53.000Z" title="发表于 2022-05-14 15:19:53">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-Ethernet-and-ARP-v7-0/" title="Wireshark Lab: Ethernet and ARP v7.0">Wireshark Lab: Ethernet and ARP v7.0</a><time datetime="2022-05-14T07:18:10.000Z" title="发表于 2022-05-14 15:18:10">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-ICMP-v7-0/" title="Wireshark Lab: ICMP v7.0">Wireshark Lab: ICMP v7.0</a><time datetime="2022-05-14T07:16:41.000Z" title="发表于 2022-05-14 15:16:41">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-NAT-v7-0/" title="Wireshark Lab: NAT v7.0">Wireshark Lab: NAT v7.0</a><time datetime="2022-05-14T07:15:13.000Z" title="发表于 2022-05-14 15:15:13">2022-05-14</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By astralcon</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script><script src="/blog/js/search/algolia.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'Uh3CYGE2t1Eaz4KoTkyVQv1N-gzGzoHsz',
      appKey: '1Gdg4rGlmT4Dcfv7oNiCH1q3',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://astralcon.gitee.io/blog/2022/05/14/Chapter-2-Variables-And-Basic-Types/'
    this.page.identifier = '2022/05/14/Chapter-2-Variables-And-Basic-Types/'
    this.page.title = 'Chapter 2: Variables And Basic Types'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }
}

if ('Valine' === 'Disqus' || !false) {
  if (false) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>